package org.finesys.gateway.handler;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.HandlerFunction;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;

import lombok.Setter;
import reactor.core.publisher.Mono;

/**
 * 网管默认首页
 */
@Setter
@ConfigurationProperties(prefix = "fine")
@Component
public class GatewayIndexHandler implements HandlerFunction<ServerResponse> {

    private String name;
    private String author;
    private String version;
    @Value("${server.port:9999}")
    private int port;

    @Override
    public Mono<ServerResponse> handle(@NonNull ServerRequest request) {
       // 获取请求的host信息
        String host=request.uri().getHost();
        //获取请求的协议信息
        String scheme=request.uri().getScheme();
        //获取请求的端口信息
        String gatewayURL=scheme+"://"+host+":"+port;
        //构建前端页面信息
        String div="<div class=\"max-w-md mx-auto p-6 bg-white rounded-lg shadow-md mt-10\">" + 
                        "      <div class=\"text-red-500 text-xl font-bold mb-4\">"+name+" gateway started!</div>" + 
                        "      <div class=\"space-y-2 mb-6 text-gray-600\">" +
                        "        <div><span class=\"mr-2\">author:</span> "+author+"</div>" +
                        "        <div><span class=\"mr-2\">version:</span> "+version+"</div>" +
                        "      </div>" +
                        "      <ul class=\"space-y-3\">" +
                        "        <li class=\"p-3 hover:bg-gray-50 rounded transition\">" +
                        "          <span class=\"font-medium\">Knife4j: </span>" +
                        "          <a href=\""+gatewayURL+"/doc.html\" " +
                        "             target=\"_blank\" rel=\"noopener noreferrer\" " +
                        "             class=\"text-blue-600 hover:text-blue-800 underline underline-offset-2 decoration-blue-500\">" +
                        "             Knife4j" +
                        "          </a>" +
                        "        </li>" +
                        "        <li class=\"p-3 hover:bg-gray-50 rounded transition\">" +
                        "          <span class=\"font-medium\">Document: </span>" +
                        "          <a href=\""+gatewayURL+"/swagger-ui.html\" " +
                        "             target=\"_blank\" rel=\"noopener noreferrer\" " +
                        "             class=\"text-blue-600 hover:text-blue-800 underline underline-offset-2 decoration-blue-500\">" +
                        "             Document" +
                        "          </a>" +
                        "        </li>" +
                        "      </ul>" +
                        "    </div>";

        return ServerResponse.status(HttpStatus.OK).contentType(MediaType.valueOf(String.valueOf(MediaType.TEXT_HTML))).body(BodyInserters.fromValue(div));
    }
}
